package com.wzz.seckillone.activeMq.base;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @ProjectName: seckillOne
 * @Package: com.wzz.seckillone.activeMq
 * @ClassName: TopicProduce
 * @Author: wzz
 * @Description: 消息生产者
 * @Date: 2022/3/20 14:49
 * @Version: 1.0
 */
@Slf4j
public class TopicProduce {
    private static final String ACTIVEMQ_URL = "tcp://192.168.1.104:61616";
    private static final String topicName = "topicOne";
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(topicName);
        MessageProducer producer = session.createProducer(topic);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();

        try {
            TextMessage message = session.createTextMessage("你好");
            producer.send(message);
            session.commit();
        }catch (Exception e){
            session.rollback();
            log.info(e.getMessage());
        }finally {
            producer.close();
            session.close();
            connection.close();
        }



    }
}
